package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: itcast_health
 * @BelongsPackage: com.itheima.health.service.impl
 * @Author: ${Shiu}
 * @CreateTime: 2020-02-21 20:16
 * @Description: 角色业务层接口
 */
@Slf4j
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;
    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {

        //设置分页插件初始参数
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //通过Dao获取Page对象
        Page<Role> page = roleDao.selectByCondition(queryPageBean.getQueryString());
        //通过Page对象，构建PageResult
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据id查询角色
     * @param id
     * @return
     */
    @Override
    public Role findById(Integer id) {
        Role role = roleDao.findById(id);
        return role;
    }

    /**
     * 根据角色id查询权限id集合
     * @param id
     * @return
     */
    @Override
    public List<Integer> findPermissionIdsById(Integer id) {
        return roleDao.findPermissionIdsById(id);
    }

    /**
     * 根据角色id查询套餐id集合
     * @param id
     * @return
     */
    @Override
    public List<Integer> findMenuIdsById(Integer id) {
        return roleDao.findMenuIdsById(id);
    }

    /**
     * 新增角色
     * @param role
     * @param permissionIds
     * @param menuIds
     */
    @Override
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
        // 添加角色,回显主键
        roleDao.add(role);
        // 根据主键id,遍历集合,向中间表添加数据
        for (Integer permissionId : permissionIds) {
            Map<String, Integer> map = new HashMap<>();
            map.put("role_Id",role.getId());
            map.put("permissionId",permissionId);
            roleDao.addRoleAndPermissions(map);
        }

        for (Integer menuId : menuIds) {
            Map<String, Integer> map = new HashMap<>();
            map.put("role_Id",role.getId());
            map.put("menuId",menuId);
            roleDao.addRoleAndMenus(map);
        }
    }

    /**
     * 删除角色
     * @param id
     */
    @Override
    public void deleteRoleById(Integer id) {
        roleDao.deletePermissionByRoleId(id);
        roleDao.deleteMenuByRoleId(id);
        roleDao.deleteRoleById(id);
    }



    /**
     * 编辑角色
     * @param role
     * @param permissionIds
     * @param menuIds
     */
    @Override
    public void edit(Role role, Integer[] permissionIds, Integer[] menuIds) {
        log.debug("permissionIds>>>>>>>>>>>>>>>>>>>>>>>>{}",permissionIds.toString());
        log.debug("menuIds>>>>>>>>>>>>>>>>>>>>>>>>{}",menuIds.toString());
        // 编辑角色数据
        roleDao.edit(role);
        // 根据角色id删除中间表中数据
        roleDao.deletePermissionByRoleId(role.getId());
        roleDao.deleteMenuByRoleId(role.getId());
        // 根据id重新添加中间表数据
        for (Integer permissionId : permissionIds) {
            Map<String, Integer> map = new HashMap<>();
            map.put("role_Id",role.getId());
            map.put("permissionId",permissionId);
            roleDao.addRoleAndPermissions(map);
        }
        for (Integer menuId : menuIds) {
            Map<String, Integer> map = new HashMap<>();
            map.put("role_Id",role.getId());
            map.put("menuId",menuId);
            roleDao.addRoleAndMenus(map);
        }
    }


    @Override
    public List<Role> findAll() {
        List<Role> list = roleDao.findAll();
        return list;
    }
}
